<?php

require_once dirname(dirname(__FILE__)) . '/config/config.php';
require_once DOCUMENT_ROOT . DIR_SHARED_CONFIG . 'db_connection.php';

class dao_journal {

    //Methods
    public function __construct() {

    }

    public function get_all() {
        $db = new db_connection();
        $con = $db->open_connect();

//        $query = "CALL GetAllJournals()";
        $query = "SELECT * FROM tbljournals";
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }

        $db->close_connect($con);
        return $list;
    }

    public function add($journal_id, $title, $thumb, $short_description, $printISSN, $onlineISSN, $publish, $photo, $impact_factor) {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "CALL addJournal(" . $journal_id . ",'" . $title . "','" . $thumb . "','" . $short_description . "','" . $printISSN . "','" . $onlineISSN . "','" . $publish . "','" . $photo . "','" . date("Y-m-d H:i:s") . "', '" . $impact_factor . "')";
        mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $db->close_connect($con);
        return true;
    }

    public function get_by_id($journal_id) {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "CALL GetJournalsById(" . $journal_id . ")";
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $row = mysqli_fetch_array($result);

        $db->close_connect($con);
        return $row;
    }

    public function delete($journal_id) {
        $db = new db_connection();
        $con = $db->open_connect();

        $query_1 = "DELETE FROM tblcategories WHERE JournalID=" . $journal_id;
        $query_2 = "DELETE FROM tbljournals WHERE JournalID=" . $journal_id;
        mysqli_query($con, $query_1) or die("Query fail: " . mysqli_error());
        mysqli_query($con, $query_2) or die("Query fail: " . mysqli_error());

        $db->close_connect($con);
        return 1;
    }

    public function search_with_title($title) {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbljournals WHERE Title like '%" . $title . "%'";
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }

        $db->close_connect($con);
        return $list;
    }

    public function search_with_first_letter($letter) {
        $db = new db_connection();
        $con = $db->open_connect();

        $query = "SELECT * FROM tbljournals WHERE Title like '" . $letter . "%'";
        $result = mysqli_query($con, $query) or die("Query fail: " . mysqli_error());

        $list = array();
        while ($row = mysqli_fetch_array($result)) {
            array_push($list, $row);
        }

        $db->close_connect($con);
        return $list;
    }

}
